/**
 * Copyright 2014 Joan Zapata
 *
 * This file is part of Android-pdfview.
 *
 * Android-pdfview is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Android-pdfview is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Android-pdfview.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.joanzapata;

import static java.lang.String.format;
import android.app.Activity;
import android.os.Bundle;

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.joanzapata.pdfview.sample.R;

public class PDFViewActivity extends Activity implements OnPageChangeListener {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		pdfView=(PDFView)findViewById(R.id.pdfView);
		afterViews();
	}
	

	public static final String SAMPLE_FILE = "aa.pdf";

	public static final String ABOUT_FILE = "about.pdf";

	PDFView pdfView;

	String pdfName = SAMPLE_FILE;

	Integer pageNumber = 1;

	void afterViews() {
		display(pdfName, false);
	}

	public void about() {
		if (!displaying(ABOUT_FILE))
			display(ABOUT_FILE, true);
	}

	private void display(String assetFileName, boolean jumpToFirstPage) {
		if (jumpToFirstPage)
			pageNumber = 1;
		setTitle(pdfName = assetFileName);

		pdfView.fromAsset(assetFileName).defaultPage(pageNumber).onPageChange(this).load();
	}

	@Override
	public void onPageChanged(int page, int pageCount) {
		pageNumber = page;
		setTitle(format("%s %s / %s", pdfName, page, pageCount));
	}

	@Override
	public void onBackPressed() {
		if (ABOUT_FILE.equals(pdfName)) {
			display(SAMPLE_FILE, true);
		} else {
			super.onBackPressed();
		}
	}

	private boolean displaying(String fileName) {
		return fileName.equals(pdfName);
	}
}
